/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mygame;

import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Sphere;
import com.jme3.scene.shape.Sphere.TextureMode;

/**
 *
 * @author fiemelantje
 */
public class CannonBallFactory {
    private static CannonBallFactory instance;
    private final Sphere sphere;
    private Material material;

    public static CannonBallFactory getInstance(AssetManager assetManager, Material material) {
        if (instance == null) {
            instance = new CannonBallFactory(assetManager, material);
        }
        return instance;
    }

    private CannonBallFactory(AssetManager assetManager, Material material) {
        sphere = new Sphere(32, 32, 0.4f, true, false);
        sphere.setTextureMode(TextureMode.Projected);
        this.material = material;

    }

    /** This method creates one individual physical cannon ball.*/
    public void makeCannonBall(Node node, BulletAppState bulletAppState, Vector3f position, Vector3f direction) {
        /** Create a cannon ball geometry and attach to scene graph. */
        Geometry ball_geo = new Geometry("cannon ball", sphere);
        ball_geo.setMaterial(material);
        node.attachChild(ball_geo);
        /** Position the cannon ball  */
        ball_geo.setLocalTranslation(position);
        /** Make the ball physcial with a mass > 0.0f */
        RigidBodyControl ball_phy = new RigidBodyControl(1f);
        /** Add physical ball to physics space. */
        ball_geo.addControl(ball_phy);
        bulletAppState.getPhysicsSpace().add(ball_phy);
        /** Accelerate the physcial ball to shoot it. */
        ball_phy.setLinearVelocity(direction.mult(50));
    }
}
